<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Picture;

class PictureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $picture = Picture::all();
        return view('picture.index', compact('picture'));
    }

    /**
     * Show the form for creating a new resource.
     * @param  int  $id  使用区域
     * @return \Illuminate\Http\Response
     * 主要负责热图的图片添加
     */
    public function create(Request $req ,$id)
    {
        if(empty($_POST)) {
            return view('picture.create', compact('id','text'));
        } else {
            $file = $req->file('pic');  //获取图片对象
            $date = date('Ymd');
            $destPath = realpath('images').$date;  //根据年月日八位数字为文件夹名字保存图片
            
            if(!$is_dir($destPath)) {
                mkdir($destPath, 0777, true);     //如该文件夹不存在则创建一个
            }
            $fileName= str_random(8).$file->getClientOriginalName();  //给上传文件重新命名
            $file->move($destPath, $fileName);    //把上传图片移动到指定目录

            $picture = new Picture();          //获取图片表的对象
            $picture->name = '热点图片';
            $destPath = '/images/'.$date.'/';
            $picture->src = $destPath.$fileName;  //图片路径
            $picture->url = '#';                  //图片超链接地址
            $picture->type = '#';
            $picture->uses = $id;                //图片使用区域  0为活动图片  不参与热图展播
            $picture->status = '0';              //图片状态
            $picture->save();
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id       id为图片的使用区域代号 0是活动图片  1是登录热图  2是首页热图
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        if($id == '1') {
            $text = '添加登录广告图';
        } elseif($id == '2') {
            $text = '添加首页广告图';
        } elseif($id == '3') {
            $text = '添加二手车广告图';
        } elseif($id == '4') {
            $text = '添加俱乐部广告图';
        } else {
            $text = '添加维修广告图';
        }
        //$picture = Picture::where('uses','=',$id)->get();
        return view('picture.list', compact('id','text'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $picture = new Picture();
        $status = $picture->status;
        if($status == '0') {
            $picture->status = '1';
        } else {
            $picture->status = '0';
        }
        $picture->save();
        $picture = Picture::where('uses','=',$id)->get();
        return view('picture.index', compact('picture'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        if(empty($_POST)) {
            $picture = Picture::find($id);
            return view('picture.update', compact('picture'));   
        } else {
            $picture = new Picture();             //获取图片表的对象
            $picture->url = $_POST['newurl'];     //图片超链接地址
            $picture->save();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $picture = Picture::find($id);
        $picture->delete();
    }
}
